package com.huilian.iotbox.admin.wap.service.impl;
import com.huilian.iotbox.admin.wap.service.DeviceService;
import com.huilian.iotbox.admin.wap.service.ExternalDeviceParamValueService;
import com.huilian.iotbox.data.cmd.PositiveDataFrameCMD;
import com.huilian.iotbox.aliyun.service.PubService;
import com.huilian.iotbox.data.cmd.VendmachPositiveDataFrameCMD;
import com.huilian.iotbox.data.common.RedisKey;
import com.huilian.iotbox.data.common.ServerResponse;
import com.huilian.iotbox.data.dto.DeviceDto;
import com.huilian.iotbox.data.dto.ExternalDeviceParamValueDto;
import com.huilian.iotbox.data.enums.DeviceItemPositionEnum;
import com.huilian.iotbox.data.vo.DeviceVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import java.io.Serializable;
import java.util.concurrent.TimeUnit;

/**
 * @author chenqiwen
 * @CreateDate: 2021-09-14-16-20
 * @Description:
 */
@Slf4j
@Service
public class ExternalDeviceParamValueServiceImpl implements ExternalDeviceParamValueService {

    @Value("${huilian.iot.productKey}")
    private String productKey;
    @Autowired
    private PubService pubService;
    @Autowired
    private DeviceService deviceService;
    @Autowired
    private RedisTemplate<String, Serializable> redisCacheTemplate;
    /**
     * 单个查询外部设备参数
     *
     * @param externalDeviceParamValueDto
     * @return
     */
    @Override
    public ServerResponse sendQueryExternalDeviceParamValue(ExternalDeviceParamValueDto externalDeviceParamValueDto) {
        if (StringUtils.isEmpty(externalDeviceParamValueDto.getDeviceId())) {
            return ServerResponse.createByErrorMessage("缺少参数");
        }

        DeviceDto deviceDto = new DeviceDto();
        deviceDto.setId(externalDeviceParamValueDto.getDeviceId());
        deviceDto.setUserInfo(externalDeviceParamValueDto.getUserInfo());
        DeviceVo deviceVo = null;
        if (deviceVo.getNetworkIntensity() <= 0) {
            return ServerResponse.createBySuccess();
        }

        final String format = String.format(RedisKey.ONE_QUERY_DEVICE_PARAM_VALUE, deviceVo.getId());
        log.info("format:" + format);
        Integer count = (Integer) redisCacheTemplate.opsForValue().get(format);
        if (!StringUtils.isEmpty(count) && count > 0) {
            log.warn(deviceVo.getDeviceNo() + "设备遇到防抖，跳过");
            return ServerResponse.createBySuccess();
        }

        String tcp = PositiveDataFrameCMD.queryBaseParams();

        String penetrate;
        if (StringUtils.isEmpty(externalDeviceParamValueDto.getHierarchy())) {
            penetrate = PositiveDataFrameCMD.penetrate(tcp);
        } else {
            penetrate = PositiveDataFrameCMD.penetrate32(DeviceItemPositionEnum.getPositionId(externalDeviceParamValueDto.getHierarchy()), tcp);
        }

        log.info("penetrate，" + penetrate);
        pubService.pub(penetrate, deviceVo.getDeviceNo(), productKey);
        redisCacheTemplate.opsForValue().set(format, 5, 5, TimeUnit.SECONDS);

        return ServerResponse.createBySuccess();
    }


    @Override
    public ServerResponse sendQueryExternalVendingDeviceParamValue(ExternalDeviceParamValueDto externalDeviceParamValueDto) {
        if (StringUtils.isEmpty(externalDeviceParamValueDto.getDeviceId())) {
            return ServerResponse.createByErrorMessage("缺少参数");
        }
        DeviceDto deviceDto = new DeviceDto();
        deviceDto.setId(externalDeviceParamValueDto.getDeviceId());
        deviceDto.setUserInfo(externalDeviceParamValueDto.getUserInfo());
        DeviceVo deviceVo = null;
        if (deviceVo.getNetworkIntensity() <= 0) {
            return ServerResponse.createBySuccess();
        }
        String tcp = VendmachPositiveDataFrameCMD.getVendingConfiguration(deviceVo.getDeviceNo());
        String command = PositiveDataFrameCMD.penetrate(tcp);
        pubService.pub(command, deviceVo.getDeviceNo(), productKey);
        return ServerResponse.createBySuccess();
    }

}
